import Constants from "./Constants";

export default class Helper {
  /**
   * 
   * @returns 
   */
  public static randomBoolen(): boolean {
    const boolean = Math.random() >= 0.5;
    return boolean;
  }

  /**
   * 获取两点间距离
   * @param p1X
   * @param p1Y
   * @param p2X
   * @param p2Y
   * @returns {number}
   */
  public static getDistance(p1X: number, p1Y: number, p2X: number, p2Y: number): number {
    var disX: number = p2X - p1X;
    var disY: number = p2Y - p1Y;
    var disQ: number = disX * disX + disY * disY;
    return Math.sqrt(disQ);
  }

  /**
  * 获取两点间角度
  * @param p1X
  * @param p1Y
  * @param p2X
  * @param p2Y
  * @returns {number}
  */
  public static getAngele(p1X: number, p1Y: number, p2X: number, p2Y: number): number {
    var disX: number = p2X - p1X;
    var disY: number = p2Y - p1Y;
    var angle: number = Math.atan2(disX, disY) / Math.PI * 180;
    return angle;
  }

  /**
* 获取两点间弧度
* @param p1X
* @param p1Y
* @param p2X
* @param p2Y
* @returns {number}
*/
  public static getRadian(p1X: number, p1Y: number, p2X: number, p2Y: number): number {
    var disX: number = p2X - p1X;
    var disY: number = p2Y - p1Y;
    var radian: number = Math.atan2(disX, disY) - Math.PI / 2;
    return radian;
  }


  /**
   * 随机a-b之间某个数 b大于a
   * @param a
   * @param b
   * @param isInteger 是否取整
   */
  public static randAtoB(a, b, isInteger = true): number {
    let rand = Math.random() * (b - a) + a;
    return isInteger ? Math.floor(rand) : rand;
  }

  /**
   * 获取排名
   * @param score
   * @param scores
   */
  static getScoreRank(score: number, scores: Array<number>): number {
    let rank = 0;
    while (score < scores[rank] && rank < scores.length) {
      rank++;
    }
    return rank + 1;
  }


  /**
   * 累加数据
   * @param target
   */
  static accumulationNumber(target: number): number {
    if (target % 2 == 0) {
      return (1 + target) * (target / 2)
    } else {
      return (1 + target) * (Math.floor(target / 2)) + Math.ceil(target / 2)
    }
  }


  /**判断物体是否超出地图外 */
  public static isOutMap(target: cc.Node) {
    return target.x > Constants.MapSize.x / 2 || target.x < -Constants.MapSize.x / 2 || target.y > Constants.MapSize.y / 2 || target.y < -Constants.MapSize.y / 2;
  }

  /**等待几秒后继续执行 */
  public static async waitForSec(sec: number) {
    return new Promise((res, rej) => {
      setTimeout(() => {
        res();
      }, sec * 1000);
    });
  }


  /**
     * 线性差值
     * @param value1 
     * @param value2 
     * @param amount 
     */
  public static lerp(value1: number, value2: number, amount: number) {
    amount = amount < 0 ? 0 : amount;
    amount = amount > 1 ? 1 : amount;
    return value1 + (value2 - value1) * amount;
  }


  /**展示界面 */
  public static showUI(name: string) {
    let path = `ui/${name}`;
    cc.loader.loadRes(path, function (err, prefab) {
      let node: cc.Node = cc.instantiate(prefab);
      cc.director.getScene().getChildByName('Canvas').addChild(node);
    });
  }

  /**展示提示界面 */
  public static showTip(tip: string) {
    let path = `ui/UITip`;
    cc.loader.loadRes(path, function (err, prefab) {
      let node: cc.Node = cc.instantiate(prefab);
      cc.director.getScene().getChildByName('Canvas').addChild(node);
      node.getComponent("UITip").content = tip;
    });
  }


  /**
     * 裁切字符串 并返回number
     * @param text 
     * @param separator 
     */
  public static splitToNumber(text: string, separator: string): any {
    if (!text || text.length == 0) {
      return []
    }

    let strArr = text.split(separator)
    let newArr = []
    for (let i = 0; i < strArr.length; i++) {
      newArr.push(Number(strArr[i]))
    }
    return newArr
  }



  /**
  * 加载resources资源
  * @param url 
  * @param type 
  */
  public static async loadRes(url, type) {
    return new Promise((res, rej) => {
      cc.loader.loadRes(url, type, (err, assets: any) => {
        if (err) {
          console.log('资源错误', url, err)
          rej(err)
        } else { res(assets) }
      });
    });
  }


  /**
  * 加载bundle资源
  * @param url 
  * @param type 
  */
  public static async loadBundle(url, type) {
    return new Promise((res, rej) => {
      cc.assetManager.loadBundle('animation', (err, bundle) => {
        bundle.load(url, type, function (err, spriteFrame) {
          if (err) {
            console.log('资源错误', url, err)
            rej(err)
          } else { res(spriteFrame) }
        });
      });
    });
  }


  /**
    * 给按钮增加回调
    * @param node 
    * @param btn 
    * @param funcName 
    * @param className 
    */
  public static addClickEvent(node: cc.Node, btn: cc.Button, funcName: string, className: string, customEventData: string = "") {
    let event = new cc.Component.EventHandler();
    event.target = node;
    event.handler = funcName;
    event.component = className;
    event.customEventData = customEventData
    btn.clickEvents.push(event);
  }


  /**
   * 判断一个点是否在一个多边形内部
   * @param pt 目标点
   * @param poly 多边形数组
   * @returns 
   */
  public static PointInPoly(pt, poly) { 
    for (var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i) 
        ((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y)) 
        && (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x) 
        && (c = !c); 
    return c; 
}




}
